package com.endlessblue.visa.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.endlessblue.visa.mapper.UserVisaMapper;
import com.endlessblue.visa.mapper.VisaHistoryMapper;
import com.endlessblue.visa.model.UserVisa;
import com.endlessblue.visa.model.UserVisaExample;
import com.endlessblue.visa.model.VisaHistory;
import com.endlessblue.visa.model.VisaHistoryExample;
import com.endlessblue.visa.service.UserVisaService;
import com.endlessblue.visa.util.DateUtil;

@Service
public class UserVisaServiceImpl implements UserVisaService{
	
	private final static Logger logger = LoggerFactory.getLogger(UserVisaServiceImpl.class);
	
	@Resource
	private UserVisaMapper userVisaMapper;
	@Resource
	private VisaHistoryMapper visaHistoryMapper;
	
	@Override
	public int countByExample(UserVisaExample example) {
		return userVisaMapper.countByExample(example);
	}

	@Override
	public int deleteByExample(UserVisaExample example) {
		return userVisaMapper.deleteByExample(example);
	}


	@Override
	public int insert(UserVisa record) {
		return userVisaMapper.insert(record);
	}

	@Override
	public int insertSelective(UserVisa record) {
		return userVisaMapper.insertSelective(record);
	}


	@Override
	public List<UserVisa> selectByExample(UserVisaExample example) {
		return userVisaMapper.selectByExample(example);
	}


	@Override
	public int updateByExampleSelective(UserVisa record, UserVisaExample example) {
		return userVisaMapper.updateByExampleSelective(record, example);
	}


	@Override
	public int updateByExample(UserVisa record, UserVisaExample example) {
		return userVisaMapper.updateByExample(record, example);
	}

	@Override
	public int updateByPrimaryKeySelective(UserVisa record) {
		return userVisaMapper.updateByPrimaryKeySelective(record);
	}


	@Override
	public int updateByPrimaryKey(UserVisa record) {
		return userVisaMapper.updateByPrimaryKey(record);
	}

	@Override
	public int deleteByPrimaryKey(String id) {
		return userVisaMapper.deleteByPrimaryKey(id);
	}

	@Override
	public UserVisa selectByPrimaryKey(String id) {
		return userVisaMapper.selectByPrimaryKey(id);
	}

	@Override
	public Boolean calcUpdateVisaLeftTime(String id) {
		UserVisa uv = userVisaMapper.selectByPrimaryKey(id);
		if (uv == null) {
			logger.error("签证:{}不存在", id);
			return false;
		}
		if (uv.getLeftTime() < 0) {
			logger.error("签证:{}已不在有效期内", id);
			return false;
		}
		VisaHistoryExample vhEx = new VisaHistoryExample();
		VisaHistoryExample.Criteria vhCr = vhEx.createCriteria();
		vhCr.andUserIdEqualTo(uv.getUserId());
		vhCr.andVisaIdEqualTo(id);
		vhEx.setOrderByClause("generate_time ASC");
		List<VisaHistory> vhList = visaHistoryMapper.selectByExample(vhEx);
		if (vhList == null || vhList.isEmpty()) {
			logger.error("签证:{}无签证记录", id);
			return false;
		}
		// 计算剩余入境有效期
		int totalInTime = calcTotalInTime(vhList);
		int leftInTime = uv.getTotalInTime() - totalInTime;
		if (leftInTime < 0) {
			leftInTime = 0;
		}
		// 计算签证剩余有效期
		int leftTime = uv.getValidity() - DateUtil.calcDays(uv.getBeginTime(), new Date());
		if (leftTime < 0) {
			leftTime = 0;
		}
		UserVisa uvUp = new UserVisa();
		uvUp.setId(id);
		uvUp.setLeftInTime(leftInTime);
		uvUp.setLeftTime(leftTime);
		uvUp.setLastVisaHisTime(vhList.get(vhList.size() - 1).getGenerateTime());
		uvUp.setLastVisaHisType(vhList.get(vhList.size() - 1).getType());
		int up = userVisaMapper.updateByPrimaryKeySelective(uvUp);
		if (up == 1) {
			return true;
		}
		return false;
	}
	
	/**
	 * 根据签证出入境记录计算到最后一次出境的入境总天数
	 * @param vhList
	 * @return
	 */
	private int calcTotalInTime (List<VisaHistory> vhList) {
		int total = 0;
		if (vhList == null || vhList.isEmpty()) {
			return total;
		}
		VisaHistory startIn = null; // 入境时间点
		VisaHistory endOut = null; // 出境时间点
		for (int i = 0; i < vhList.size(); i++) {
			VisaHistory curr = vhList.get(i);
			if (startIn == null && curr.getType() != 1) {
				continue;
			}
			if (startIn == null && curr.getType() == 1) {
				startIn = curr;
				continue;
			}
			if (curr.getType() != -1) {
				continue;
			}
			endOut = curr;
			total += DateUtil.calcDays(startIn.getGenerateTime(), endOut.getGenerateTime());
			startIn = null;
			endOut = null;
		}
		return total;
	}
}
